<?php

function __autoload($class_name) 
    {
        include $class_name . '.php';
    }
    $page = new page("Editovat uzivatele");
    
    $content='';  
    $error = '';
    $user_to_edit = $_SESSION['user'];//defaultni hodnota
    if(isset($_SESSION['user']) && $_SESSION['user']->validate())
    {
        //zkontrolovat zmenu na jinem uzivateli - provadi admin
        if(isset($_GET['id'])&& ($_SESSION['user']->getRights() == 3))
        {
            //admin meni uzivate s id
            $user_to_edit = new user($_GET['id']);
            $content .= "zmena uzivatele s id: ". $user_to_edit->getId()."<br />";
        }
        //stranka pro registrovaneho uzivatele
        //kontrola jiz zadanych udaju ke zmene
        if(isset($_POST['editUser']))
        {
            if(isset($_POST['password']))                    
            {
                if($_POST['password']!=$_POST['passwordConfirm'])
                {
                    $error .= "Chybne zadane heslo!<br />";
                    unset($_POST['editUser']);
                }
            }
        }
        if(isset($_POST['editUser']))//byl vyplnen formular
        {
            //pokud v zadanych udajich nebyly chyby,
            //naplnit spravne hodnoty pro zmenu v databazi
            
            $changed = false;
            $new_password=null;
            $new_name=null;
            $new_email=null;
            $new_rights=null;
            $new_state=null;
            if($_POST['password'] != "")
            {
                $changed = true;
                $new_password = $_POST['password'];
            }  
            if($_POST['name']!="")
            {
                $changed = true;
                $new_name = $_POST['name'];
            }
            if($_POST['email']!="")
            {
                $changed = true;
                $new_email = $_POST['email'];
            }
            if($_SESSION['user']->getRights() == 3)
            {
                if(isset($_POST['rights']) && $_POST['rights']!="")
                {
                    $changed = true;
                    $new_rights = $_POST['rights'];
                }
                if(isset($_POST['state']) && $_POST['state']!="")
                {
                    $changed = true;
                    $new_state = $_POST['state'];
                }
            }
            if($changed == true)//aktualizovat zaznam v databazi
            {
                $content .= "Ted zmena u : ".$user_to_edit->getId()."<br />";
                $user_to_edit->updateInfo($new_password,
                        $new_name,
                        $new_email,
                        $new_rights,
                        $new_state);
            }
            $content .="Udaje byly aktualizovany! <br />";
        }else
        {
            //prvni zobrazeni nebo byly chyby ve formulari
            $content .= $error;
            $content .= "Platne udaje pro uzivatele ". $user_to_edit->getLogin() . " : <br />";
            $content .= "Jmeno : ". $user_to_edit->getName() . "<br />";
            $content .= "Email : ". $user_to_edit->getEmail() . "<br />";
            //formular pro zmeny
            $to_change = array(
                        new formPassword("password", 'Nove heslo : '),
                        new formPassword("passwordConfirm", 'Nove heslo znovu : '),
                        new formText("name", 'Zmente Vase jmeno : '),
                        new formText("email", 'Zmente Vas email : ')
                        );            
            if($_SESSION['user']->getRights() == 3)
            {
                $content .= "Opravneni: ". $user_to_edit->getRights() . "<br />";
                $content .= "Stav: ".$user_to_edit->getState()."<br />";
                array_push($to_change, new formText("rights",'Zmena pristupovaych prav: '));
                array_push($to_change, new formtext("state", 'Zmena stavu uctu :'));
            }
            array_push($to_change, new formSubmit("Potvrdit zmeny"));
            
            
            $edit_form = new form(
                    "editUser",
                    "editUser.php?id=".$user_to_edit->getId(),
                    "post",
                    "zmena udaju uzivatele :",
                    $to_change
                        
                    ); 
            $content .= $edit_form->render();
        }
        
    }else
    {
        //TODO: stranka pro hosta
        
        $content .= "Nejste prihlaseni!";
        $content .= "<br />";
        $content .= "prihlaste se!";
        $content .= "<br />";
        
    }
    echo $page->render($content);
?>
